package ying.cookie03;
/**
 * ClassName: GetCookie
 * Package: ying.cookie03
 * Description:
 *
 * @Author 刘英杰
 * @Create 2024/10/5 9:39
 * @Version 1.0
 */

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import ying.utils.CookieUtils;

import java.io.IOException;

/**
 * 获取使用默认生命周期的Cookie
 */

@WebServlet("/getCookie")
public class GetCookie extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Cookie[] cookies = request.getCookies();
        Cookie cookie = CookieUtils.getCookie(cookies, "id");
        response.getWriter().write(cookie==null? "null" :cookie.getValue());
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
